package org.stephen.recommend.controller;

import org.stephen.recommend.common.RedisKeyPrefix;
import org.stephen.recommend.enums.LanguageType;
import org.stephen.recommend.model.UserTags;
import org.stephen.recommend.service.CmsArticleService;
import org.stephen.recommend.util.RedisUtils;
import org.stephen.recommend.util.SerializeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import java.util.Set;

/**
 * ClassName: testController
 * Description:
 * Date: 2020/12/16 17:57
 *
 * @author stephen.qiu
 */
@RestController
public class TestController {
    @Autowired
    RedisUtils redisUtils;

    @Autowired
    CmsArticleService cmsArticleService;

    @PostConstruct
    public void init() {
        Set<String> keys = redisUtils.keys(RedisKeyPrefix.getUserTag(LanguageType.ZH_CN) + "*");
        for (String key : keys) {
            UserTags userTags = (UserTags) SerializeUtils.stringSerializeObject(redisUtils.get(key));
            if (userTags == null) {
                System.out.println("序列号版本号错误");
                clear();
            } else {
                System.out.println("序列号版本号正确");
            }
            break;
        }
    }

    @GetMapping(value = "clear")
    public String clear() {
        Set<String> keys = redisUtils.keys(RedisKeyPrefix.VIEW_ARTICLE_IDS + "*");
        keys.addAll(redisUtils.keys(RedisKeyPrefix.RECOMMENDED_ARTICLE_IDS + "*"));
        keys.addAll(redisUtils.keys(RedisKeyPrefix.SIMILARITY_USERS + "*"));
        keys.addAll(redisUtils.keys(RedisKeyPrefix.getUserTag(LanguageType.EN_US) + "*"));
        keys.addAll(redisUtils.keys(RedisKeyPrefix.getUserTag(LanguageType.ZH_CN) + "*"));
        keys.addAll(redisUtils.keys(RedisKeyPrefix.VIEW_ARTICLE_IDS + "*"));
        for (String key : keys) {
            redisUtils.delete(key);
            System.out.println("删除" + key + "成功!");
        }
        System.out.println("删除推荐系统redis数据成功!");
        return "success";
    }

    @GetMapping(value = "clearTag")
    public String clearTag() {
        Set<String> keys = redisUtils.keys(RedisKeyPrefix.getTag(LanguageType.EN_US) + "*");
        keys.addAll(redisUtils.keys(RedisKeyPrefix.getTag(LanguageType.ZH_CN) + "*"));
        keys.addAll(redisUtils.keys("tag:*"));
        for (String key : keys) {
            redisUtils.delete(key);
            System.out.println("删除" + key + "成功!");
        }
        System.out.println("删除推荐系统redis Tag数据成功!");
        return "success";
    }
}
